/*
Copyright (C) 2003 Michael Würtinger

This file is part of Dynamicengine.

Dynamicengine is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

Dynamicengine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Dynamicengine. If not, see http://www.gnu.org/licenses/.
*/


#include "include.h"
#include "display.h"
#include "keyboard.h"
#include "physics.h"
#include "reader.h"
#include "main.h"



CSite Site;
//CStructFile File;


int main(int argc, char** argv)
{
/*	if(argc > 1)
		File.ReadFile(argv[1]);
	else
		File.ReadFile("struct.str");

	File.Translate(&Site);
*/
	srand(time(NULL));

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(20, 20);
	glutCreateWindow("Engine");
	glutFullScreen();
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutIdleFunc(idle);

	make_bendbridge();
	
	glutMainLoop();

	return 0;
}

void restart()
{
//	Site.Clear();
//	File.Translate(&Site);
//	make_bendbridge();
}

void idle()
{
	static int d;
	d++;

	calculateFPS();

	if(d > 100)
	{	
		display();
		d = 0;
	}

	Site.Simulate(0.0001);
}

void app_exit(int code)
{
	glutReshapeWindow(800, 600);
	exit(code);
}

void make_bridge()
{
	CJoint* pJoint01 = Site.CreateJoint(false, vec3f(-3.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint02 = Site.CreateJoint(false, vec3f(-2.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint03 = Site.CreateJoint(false, vec3f(-1.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint04 = Site.CreateJoint(false, vec3f( 0.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint05 = Site.CreateJoint(false, vec3f( 1.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint06 = Site.CreateJoint(false, vec3f( 2.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint07 = Site.CreateJoint(true , vec3f( 3.0, 2.5, 0.0), vec3f(0.0, 0.0, 0.0));
	
	CJoint* pJoint08 = Site.CreateJoint(false, vec3f(-2.0, 3.0, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint09 = Site.CreateJoint(false, vec3f(-1.0, 3.0, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint10 = Site.CreateJoint(false, vec3f( 0.0, 3.0, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint11 = Site.CreateJoint(false, vec3f( 1.0, 3.0, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint12 = Site.CreateJoint(false, vec3f( 2.0, 3.0, 0.0), vec3f(0.0, 0.0, 0.0));

	Site.CreateTruss(pJoint01, pJoint02, &mat_steel);
	Site.CreateTruss(pJoint02, pJoint03, &mat_steel);
	Site.CreateTruss(pJoint03, pJoint04, &mat_steel);
	Site.CreateTruss(pJoint04, pJoint05, &mat_steel);
	Site.CreateTruss(pJoint05, pJoint06, &mat_steel);
	Site.CreateTruss(pJoint06, pJoint07, &mat_steel);
	Site.CreateTruss(pJoint08, pJoint09, &mat_steel);
	Site.CreateTruss(pJoint09, pJoint10, &mat_steel);
	Site.CreateTruss(pJoint10, pJoint11, &mat_steel);
	Site.CreateTruss(pJoint11, pJoint12, &mat_steel);
	
	Site.CreateTruss(pJoint02, pJoint08, &mat_steel);
	Site.CreateTruss(pJoint03, pJoint09, &mat_steel);
	Site.CreateTruss(pJoint04, pJoint10, &mat_steel);
	Site.CreateTruss(pJoint05, pJoint11, &mat_steel);
	Site.CreateTruss(pJoint06, pJoint12, &mat_steel);
	Site.CreateTruss(pJoint01, pJoint08, &mat_steel);
	Site.CreateTruss(pJoint08, pJoint03, &mat_steel);
	Site.CreateTruss(pJoint03, pJoint10, &mat_steel);
	Site.CreateTruss(pJoint10, pJoint05, &mat_steel);
	Site.CreateTruss(pJoint05, pJoint12, &mat_steel);
	Site.CreateTruss(pJoint12, pJoint07, &mat_steel);
}

void make_bendbridge()
{
	CJoint* pJoint01 = Site.CreateJoint(false, vec3f(-3.0, -4.9, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint02 = Site.CreateJoint(false, vec3f(-2.0, -4.4, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint03 = Site.CreateJoint(false, vec3f(-1.0, -4.1, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint04 = Site.CreateJoint(false, vec3f( 0.0, -4.0, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint05 = Site.CreateJoint(false, vec3f( 1.0, -4.1, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint06 = Site.CreateJoint(false, vec3f( 2.0, -4.4, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint07 = Site.CreateJoint(false, vec3f( 3.0, -4.9, 0.0), vec3f(0.0, 0.0, 0.0));
	
	CJoint* pJoint08 = Site.CreateJoint(false, vec3f(-2.0, -3.9, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint09 = Site.CreateJoint(false, vec3f(-1.0, -3.6, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint10 = Site.CreateJoint(false, vec3f( 0.0, -3.5, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint11 = Site.CreateJoint(false, vec3f( 1.0, -3.6, 0.0), vec3f(0.0, 0.0, 0.0));
	CJoint* pJoint12 = Site.CreateJoint(false, vec3f( 2.0, -3.9, 0.0), vec3f(0.0, 0.0, 0.0));

	Site.CreateTruss(pJoint01, pJoint02, &mat_steel);
	Site.CreateTruss(pJoint02, pJoint03, &mat_steel);
	Site.CreateTruss(pJoint03, pJoint04, &mat_steel);
	Site.CreateTruss(pJoint04, pJoint05, &mat_steel);
	Site.CreateTruss(pJoint05, pJoint06, &mat_steel);
	Site.CreateTruss(pJoint06, pJoint07, &mat_steel);
	Site.CreateTruss(pJoint08, pJoint09, &mat_steel);
	Site.CreateTruss(pJoint09, pJoint10, &mat_steel);
	Site.CreateTruss(pJoint10, pJoint11, &mat_steel);
	Site.CreateTruss(pJoint11, pJoint12, &mat_steel);
	
	Site.CreateTruss(pJoint02, pJoint08, &mat_steel);
	Site.CreateTruss(pJoint03, pJoint09, &mat_steel);
	Site.CreateTruss(pJoint04, pJoint10, &mat_steel);
	Site.CreateTruss(pJoint05, pJoint11, &mat_steel);
	Site.CreateTruss(pJoint06, pJoint12, &mat_steel);
	Site.CreateTruss(pJoint01, pJoint08, &mat_steel);
	Site.CreateTruss(pJoint08, pJoint03, &mat_steel);
	Site.CreateTruss(pJoint03, pJoint10, &mat_steel);
	Site.CreateTruss(pJoint10, pJoint05, &mat_steel);
	Site.CreateTruss(pJoint05, pJoint12, &mat_steel);
	Site.CreateTruss(pJoint12, pJoint07, &mat_steel);
}
